package com.itbaizhan;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.bson.Document;

import java.util.ArrayList;
/*用java 连接mongodb 不用编写config.properties*/
public class MongoDBExample {
    public static void main(String[] args) {
        //获取mongoDb连接地址
        String connectionString = System.getProperty("mongodb.uri");
        try{
            //用mongodb的服务连接
            MongoClient mongoClient = MongoClients.create(connectionString);
            ArrayList<Document> databases = mongoClient.listDatabases().into(new ArrayList<>());
            //用for:each遍历，lambda表达式打印
            databases.forEach(db -> System.out.println(db.toJson()));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
/*点击Run --> Run...  --> edit configurations  -->
* VM options :  -Dmongodb.uri="mongodb://192.168.66.105:27017/?maxPoolSize=2&w=majority"
* maxPoolSize=2  最大连接池为2
* w=majority   适用于集群架构，要求写入操作已经传递到绝大多数投票节点以及主节点后进行应答
* */
